package com.daoImpl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.LockMode;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.bean.ApplicablePart;
import com.bean.ProductPart;
import com.dao.ProductPartDao;

/**
 * A data access object (DAO) providing persistence and search support for
 * ProductPart entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.bean.ProductPart
 * @author MyEclipse Persistence Tools
 */
public class ProductPartDaoImpl extends HibernateDaoSupport implements ProductPartDao {
	private static final Logger log = LoggerFactory
			.getLogger(ProductPartDaoImpl.class);
	// property constants
	public static final String PART_CODE = "partCode";
	public static final String PRODUCT_CODE = "productCode";
	public static final String PHOTO = "photo";
	public static final String SIZE_RANGE = "sizeRange";
	public static final String TOTAL_PRICE = "totalPrice";
	public static final String MEMO = "memo";
	public static final String STATUS = "status";
	public static final String SYS_USER = "sysUser";

	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.ProductPartDao#save(com.bean.ProductPart)
	 */
	@Override
	public void save(ProductPart transientInstance) {
		log.debug("saving ProductPart instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.ProductPartDao#delete(com.bean.ProductPart)
	 */
	@Override
	public void delete(ProductPart persistentInstance) {
		log.debug("deleting ProductPart instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.ProductPartDao#findById(java.lang.Integer)
	 */
	@Override
	public ProductPart findById(java.lang.Integer id) {
		log.debug("getting ProductPart instance with id: " + id);
		try {
			ProductPart instance = (ProductPart) getHibernateTemplate().get(
					"com.bean.ProductPart", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.ProductPartDao#findByExample(com.bean.ProductPart)
	 */
	@Override
	public List findByExample(ProductPart instance) {
		log.debug("finding ProductPart instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.ProductPartDao#findByProperty(java.lang.String, java.lang.Object)
	 */
	@Override
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding ProductPart instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from ProductPart as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.ProductPartDao#findByPartCode(java.lang.Object)
	 */
	@Override
	public List findByPartCode(Object partCode) {
		return findByProperty(PART_CODE, partCode);
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.ProductPartDao#findByProductCode(java.lang.Object)
	 */
	@Override
	public List findByProductCode(Object productCode) {
		return findByProperty(PRODUCT_CODE, productCode);
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.ProductPartDao#findByPhoto(java.lang.Object)
	 */
	@Override
	public List findByPhoto(Object photo) {
		return findByProperty(PHOTO, photo);
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.ProductPartDao#findBySizeRange(java.lang.Object)
	 */
	@Override
	public List findBySizeRange(Object sizeRange) {
		return findByProperty(SIZE_RANGE, sizeRange);
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.ProductPartDao#findByTotalPrice(java.lang.Object)
	 */
	@Override
	public List findByTotalPrice(Object totalPrice) {
		return findByProperty(TOTAL_PRICE, totalPrice);
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.ProductPartDao#findByMemo(java.lang.Object)
	 */
	@Override
	public List findByMemo(Object memo) {
		return findByProperty(MEMO, memo);
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.ProductPartDao#findByStatus(java.lang.Object)
	 */
	@Override
	public List findByStatus(Object status) {
		return findByProperty(STATUS, status);
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.ProductPartDao#findBySysUser(java.lang.Object)
	 */
	@Override
	public List findBySysUser(Object sysUser) {
		return findByProperty(SYS_USER, sysUser);
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.ProductPartDao#findAll()
	 */
	@Override
	public List findAll() {
		log.debug("finding all ProductPart instances");
		try {
			String queryString = "from ProductPart";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.ProductPartDao#merge(com.bean.ProductPart)
	 */
	@Override
	public ProductPart merge(ProductPart detachedInstance) {
		log.debug("merging ProductPart instance");
		try {
			ProductPart result = (ProductPart) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.ProductPartDao#attachDirty(com.bean.ProductPart)
	 */
	@Override
	public void attachDirty(ProductPart instance) {
		log.debug("attaching dirty ProductPart instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.ProductPartDao#attachClean(com.bean.ProductPart)
	 */
	@Override
	public void attachClean(ProductPart instance) {
		log.debug("attaching clean ProductPart instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static ProductPartDao getFromApplicationContext(
			ApplicationContext ctx) {
		return (ProductPartDao) ctx.getBean("productPartDao");
	}

	@Override
	public List<ProductPart> findProductParts(String table, String pk,
			String sort, int PageNumber, int PageSize, String querys) {
		Session session = getSession();  
		SQLQuery query=null;
		
		StringBuffer sqlbuf = new StringBuffer("execute [dbo].[sp_get_page_of_table] ");
		sqlbuf.append("'"+table+"','"+pk+"','"+sort+"',"+PageNumber+","+PageSize+",'*',\""+querys+"\"");
		
		query=session.createSQLQuery(sqlbuf.toString());
		query.addEntity(ProductPart.class);
		query.addEntity(ApplicablePart.class);
		 
		List<Object[]> resultSet = query.list();
		List<ProductPart> list = new ArrayList<ProductPart>();
		
		for (Object[] r : resultSet) {
			ProductPart pdp = (ProductPart)r[0];
			ApplicablePart ap = (ApplicablePart)r[1];
			pdp.setApplicablePart(ap);
			
			list.add(pdp);
		}
		
		return list;
	}
}